package com.hgyc.mom.eam.controller;

import java.util.*;

import com.hgyc.mom.eam.vo.EamEquipmentRepairVO;
import jakarta.servlet.http.HttpServletResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.BeanUtils;
import org.springframework.web.multipart.MultipartFile;
import com.hgyc.mom.eam.entity.EamEquipmentRepair;
import com.hgyc.mom.eam.vo.EamEquipmentRepairParam;
import com.hgyc.mom.eam.service.EamEquipmentRepairService;
import com.hgyc.mom.common.vo.Result;
import com.hgyc.mom.common.util.poi.ExcelUtil;

import com.hgyc.mom.core.mybatis.QueryWrapperUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * 设备维修单Controller
 *
 * @author fwj
 * @since 2025-08-18
 */
@Tag(name = "设备维修单控制器", description = "设备维修单控制器")
@RestController
@RequestMapping("/eam/equipment/repair")
public class EamEquipmentRepairController {
    @Autowired
    private EamEquipmentRepairService eamEquipmentRepairService;

    @Operation(summary = "查询设备维修单列表", description = "查询设备维修单列表")
    @PostMapping("/pageList")
    public Result<IPage<EamEquipmentRepair>> pageList(@Parameter(description = "查询参数") @RequestBody EamEquipmentRepairParam eamEquipmentRepairParam) {
        Page<EamEquipmentRepair> page = new Page<>(eamEquipmentRepairParam.getPage(), eamEquipmentRepairParam.getPageSize());
        EamEquipmentRepair eamEquipmentRepair = new EamEquipmentRepair();
        BeanUtils.copyProperties(eamEquipmentRepairParam, eamEquipmentRepair);
        QueryWrapper<EamEquipmentRepair> wrapper = QueryWrapperUtil.build(eamEquipmentRepair);
        IPage<EamEquipmentRepair> list = eamEquipmentRepairService.page(page, wrapper);
        return Result.success(list);
    }


    @Operation(summary = "获取设备维修单详细信息", description = "获取设备维修单详细信息")
    @GetMapping(value = "/{id}")
    public Result<EamEquipmentRepair> getInfo(@Parameter(description = "设备维修单ID") @PathVariable("id") Long id) {
        return Result.success(eamEquipmentRepairService.getById(id));
    }
    @Operation(summary = "新增设备维修单", description = "新增设备维修单")
    @PostMapping
    public Result<?> save(@Parameter(description = "设备维修单") @RequestBody EamEquipmentRepair eamEquipmentRepair) {
        eamEquipmentRepairService.saveRepair(eamEquipmentRepair);
        return Result.success();
    }

    @Operation(summary = "修改设备维修单", description = "修改设备维修单")
    @PutMapping
    public Result<?> update(@Parameter(description = "设备维修单") @RequestBody EamEquipmentRepair eamEquipmentRepair) {
        eamEquipmentRepairService.updateRepair(eamEquipmentRepair);
        return Result.success();
    }

    @Operation(summary = "删除设备维修单", description = "删除设备维修单")
    @DeleteMapping
    public Result<?> remove(@Parameter(description = "删除ID") @RequestParam("ids") Long[] ids) {
        eamEquipmentRepairService.removeRepair(Arrays.asList(ids));
        return Result.success();
    }

    @Operation(summary = "开始维修", description = "开始维修")
    @PostMapping("/start/{id}")
    public Result<?> startRepair(@PathVariable("id") Long id) {
        eamEquipmentRepairService.startRepair(id);
        return Result.success();
    }

    @Operation(summary = "保存维修记录", description = "新增设备维修单")
    @PostMapping("/saveRecord")
    public Result<?> saveRecord(@Parameter(description = "设备维修单") @RequestBody EamEquipmentRepairVO eamEquipmentRepairVO) {
        eamEquipmentRepairService.saveRecord(eamEquipmentRepairVO);
        return Result.success();
    }

    @Operation(summary = "下载导入模版", description = "下载导入模版")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<EamEquipmentRepair> util = new ExcelUtil<>(EamEquipmentRepair.class);
        util.importTemplateExcel(response, "设备维修单");
    }

    @Operation(summary = "导出设备维修单列表", description = "导出设备维修单列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, EamEquipmentRepairParam eamEquipmentRepairParam) {
        EamEquipmentRepair eamEquipmentRepair = new EamEquipmentRepair();
        BeanUtils.copyProperties(eamEquipmentRepairParam, eamEquipmentRepair);
        QueryWrapper<EamEquipmentRepair> wrapper = QueryWrapperUtil.build(eamEquipmentRepair);
        List<EamEquipmentRepair> list = eamEquipmentRepairService.list(wrapper);
        ExcelUtil<EamEquipmentRepair> util = new ExcelUtil<>(EamEquipmentRepair.class);
        util.exportExcel(response, list, "设备维修单数据");
    }

    @Operation(summary = "导入设备维修单列表", description = "导入设备维修单列表")
    @PostMapping("/import")
    public Result<?> importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<EamEquipmentRepair> util = new ExcelUtil<>(EamEquipmentRepair.class);
        List<EamEquipmentRepair> eamEquipmentRepairList = util.importExcel(file.getInputStream());
        String message = eamEquipmentRepairService.importData(eamEquipmentRepairList, updateSupport);
        return Result.success(message);
    }
}
